function loadInfo(page) {
  wx.getLocation({
    type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标 
    success: function (res) {
      // success 
      var longitude = res.longitude
      var latitude = res.latitude
      loadCity(longitude, latitude,page)
    },
    fail: function () {
      // fail
      var item = page.data.item;
      item.userLocation = false;
      page.setData({
        item: item
      })
      wx.showToast({
        title: '定位失败',
        icon: 'fail',
        duration: 1000
      })
    },
    complete: function () {
      // complete 
    }
  })
}

function loadCity(longitude, latitude,page) {
  wx.request({
    url: 'https://api.map.baidu.com/geocoder/v2/?ak=PrCz399EZgV7t3aKsGDx3BCOFTIGCOMI&location=' + latitude + ',' + longitude + '&output=json&pois=1',
    //这里的ak 是去百度地图api获取的需要自己登陆获取一下 地址：https://lbsyun.baidu.com/index.php?title=wxjsapi
    data: {},
    header: {
      'Content-Type': 'application/json'
    },
    success: function (res) {
      // success 
      // console.log(res);
      var city = res.data.result.addressComponent.city;
      var sub1 = city.substring(city.length - 1, city.length);
      if (sub1 = '市') {
        city = city.substring(0, city.length - 1);
      }
      var item = page.data.item;
      item.currentCity = city;
      item.defaultVal = city;
      item.userLocation = true
      page.setData({ item: item });
      var str1 = 'item.city_isClicked';
      var str2 = 'item.country_isClicked';
      page.setData({
        [str1]: true,
        [str2]: false
      })
    },
    fail: function () {
      // fail 
    },
    complete: function () {
      // complete 
    }
  })
}
module.exports = {
  loadInfo: loadInfo,
  loadCity: loadCity
}